/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.worlds.object;

import net.worlds.WorldsException;
import net.worlds.Main;
import net.worlds.PropChangeHandler;
import net.worlds.lua.Event;
import se.krka.kahlua.vm.JavaFunction;
import se.krka.kahlua.vm.LuaCallFrame;


public class Player extends GameObject{
    private Connection connection;
    private String message = null;
    public Player() throws WorldsException
    {
        super("Player");
        addPropChangeHandler("Speed", new PropChangeHandler()
        {
            @Override
            public void handle(Object oldValue, Object value) throws WorldsException {
                if(Main.getLocalPlayer() != null)
                {
                    Main.getLocalPlayer().setSpeed((Float)value);
                }
            }
        });
        addPropChangeHandler("Character", new PropChangeHandler()
        {

            @Override
            public void handle(Object oldValue, Object value) throws WorldsException {
                if(value != null)
                {
                    if(oldValue == null)
                        getEvent("CharacterLoaded").fire(new Object[]{value});
                    if(Main.getLocalPlayer() != null && Main.getLocalPlayer().getPlayer() == instance)
                        Main.setLocalPlayer((Player) instance);
                }
            }

        });
        addPropChangeHandler("ShowUI", new PropChangeHandler()
        {
            @Override
            public void handle(Object oldValue, Object value) throws WorldsException {
                boolean v = (Boolean) value;
                if(!Main.noView)
                    Main.getView().showUI = v;
            }
        });
        //x_;
        addFunction("getConnection", new JavaFunction()
        {

            public int call(LuaCallFrame callFrame, int nArguments) {
                callFrame.push(connection);
                return 1;
            }

        });
        addFunction("setConnection", new JavaFunction()
        {

            public int call(LuaCallFrame callFrame, int nArguments) {
                setConnection((Connection)callFrame.get(1));
                return 0;
            }

        });
        addFunction("message", new JavaFunction()
        {

            public int call(LuaCallFrame callFrame, int nArguments) {
                if(nArguments == 1)
                {
                    message = null;
                        if(Main.isServer())
                        {
                            Main.getServer().sendPlayerMessage((Player) instance, "");
                        }
                    return 0;
                }
                else if(nArguments != 2 || !(callFrame.get(1) instanceof String))
                    throw new RuntimeException("Argument to message() must be a string");
                final String s = (String) callFrame.get(1);
                Main.runInMainThread(new Runnable()
                {

                    public void run() {
                        message = s;
                    }

                });
                if(Main.isServer())
                {
                    Main.getServer().sendPlayerMessage((Player) instance, s);
                }
                return 0;
            }

        });
        addFunction("kick", new JavaFunction()
        {
            public int call(LuaCallFrame callFrame, int nArguments) {
                String msg;
                if(nArguments >= 2)
                    msg = callFrame.get(1).toString();
                else
                    msg = "You were kicked!";
                if(Main.isServer())
                    Main.getServer().kick((Player) instance, msg);
                else
                {
                    try {
                        if (Main.isClient()) {
                            Main.getClient().disconnect();
                        }
                        Main.setLocalPlayer(null);
                        Main.loading = true;
                        world.setMessage(msg);
                    } catch (WorldsException ex) {
                        ex.printStackTrace();
                    }
                }
                return 0;
            }
        });
        addEvent("Died", new Event(), "none");
        addEvent("Chatted", new Event(), "message (string)");
        addEvent("CharacterLoaded", new Event(), "none");
        addFunction("setTool", new JavaFunction()
        {
            public int call(LuaCallFrame callFrame, int nArguments) {
                if(nArguments != 2 || !(callFrame.get(1) instanceof Double))
                    throw new RuntimeException("setTool() requires an integer argument");
                if(Main.isServer())
                    throw new RuntimeException("This function may only be used in client-side scripts");
                if(Main.getLocalPlayer() != null)
                {
                    double d = (Double) callFrame.get(1) - 1;
                    int idx = (int) d;
                    if(idx != Main.getLocalPlayer().getSelectedToolIndex())
                    {
                        if(idx == -2)
                            idx = Main.getLocalPlayer().getSelectedToolIndex();
                        final int fidx = idx;
                        if(idx >= 0)
                            Main.runInMainThread(new Runnable()
                            {
                                public void run() {
                                    try {
                                        Main.getLocalPlayer().setSelectedTool(fidx);
                                    } catch (WorldsException ex) {
                                        ex.printStackTrace();
                                    }
                                }
                            });
                    }
                }
                return 0;
            }
        });
    }
    public PlayerStats getStats()
    {
        try {
            if(hasChild("Stats") && getChild("Stats") instanceof PlayerStats)
            {
                return (PlayerStats) getChild("Stats");
            }
            return null;
        } catch (WorldsException ex) {
            return null;
        }
    }
    public void setMessage(String msg)
    {
        message = msg;
    }
    @Override
    public void destructor() throws WorldsException
    {
        super.destructor();
    }
    public void setConnection(Connection c)
    {
        connection = c;
    }
    public Connection getConnection()
    {
        return connection;
    }
    public String getMessage()
    {
        return message;
    }
}
